14 Replies - 10675 Views - Last Post: 15 January 2013 - 10:19 AM Rate Topic: -----

#1 Agustinbmed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-January 13

Creating a class with dynamically allocated integers

Posted 14 January 2013 - 03:10 PM

Hi,

I know this question may seem vague... But I wasn't given much information either... So please bear with me please.

I had an interview for an embedded systems position, and although I know how to program in c and c++, I haven't been involved in many complex projects....
So I had a question that was the following:

Write a class that dynamically allocates buffers or integers and that it is thread safe.


So I had no idea what to write, my background is in biomedical and electrical, not systems or computer science, a d although I have meddled with micro controllers such as arduinos, I haven't really done heavy programming.

I kind of think it has something to do with malloc or dalloc, but never actually encountered this code before.

I ask of you how should I have answered this? Not sure if this question is going to be the decisive reason to not hire me but at least I would like to know the answer to it.

I appreciate your help a d consideration!

Cheers

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a class with dynamically allocated integers

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 765
  • View blog
  • Posts: 2,227
  • Joined: 20-March 10

Re: Creating a class with dynamically allocated integers

Posted 14 January 2013 - 05:08 PM

hmm,

What do you think would be the easiest way to make it

'thread safe'

the clue is right there..

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#3 Agustinbmed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-January 13

Re: Creating a class with dynamically allocated integers

Posted 14 January 2013 - 05:51 PM

It would be deleting it... But as I mentioned I'm not really a programmer so I'm guessing it would use some delete function... But I was also reading and other people use pointers... Like:

 

Class  dynamAlloc
{

int * p;
p= new int;

delete p;

};




Would this as simple as this have worked?

Thanks for your help
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 765
  • View blog
  • Posts: 2,227
  • Joined: 20-March 10

Re: Creating a class with dynamically allocated integers

Posted 14 January 2013 - 08:19 PM

No,

This is an example of thread safety

#include <pthread.h>
 
int increment_counter ()
{
        static int counter = 0;
        static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 
        pthread_mutex_lock(&mutex);
 
        // only allow one thread to increment at a time
        ++counter;
        // store value before any other threads increment it further
        int result = counter;   
 
        pthread_mutex_unlock(&mutex);
 
        return result;
}





but this is also thread safe using a vector

 

Class  dynamAlloc
{

vector<int> p;
public:

void Set(int a)
{
p.pushback(a);
}
int Get(int d)
{
return p[d];
}
};



This post has been edited by snoopy11: 14 January 2013 - 08:21 PM

Was This Post Helpful? 1
  • +
  • -

#5 Agustinbmed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-January 13

Re: Creating a class with dynamically allocated integers

Posted 14 January 2013 - 08:27 PM

Wow, I really appreciate your help!

This will probably give me nightmares for a few weeks...

The vector way seems a bit easier, is this type of programming very common?
I'm mostly used to easy algorithms in MatLab and data acquisition algorithms...

Thanks a lot for the help! I will look up what pushback does...

I really really appreciate your help!
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 765
  • View blog
  • Posts: 2,227
  • Joined: 20-March 10

Re: Creating a class with dynamically allocated integers

Posted 14 January 2013 - 08:42 PM

Vector Reference

Please read this...
Was This Post Helpful? 1
  • +
  • -

#7 Agustinbmed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-January 13

Re: Creating a class with dynamically allocated integers

Posted 14 January 2013 - 08:52 PM

You Sir are an angel, this is gold right here... Thanks!

Just a quick glance cleared a lot of doubts, ill read it through!

May the force be with you!
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,742
  • Joined: 05-May 12

Re: Creating a class with dynamically allocated integers

Posted 15 January 2013 - 05:11 AM

As far as I know the Standard Template Libraries are not required to be thread safe. So using the vector within the class won't guarantee thread safety.

It will depend on which implementation of the STL you are using. For example if you are using SGI's they will only guarantee read thread safety for simultaneous read operations, but not for read and write operations.
( http://www.sgi.com/t...ead_safety.html ) It is similar for the Microsoft implementation of the STL. ( http://msdn.microsof...b(v=vs.80).aspx )

This post has been edited by Skydiver: 15 January 2013 - 05:19 AM

Was This Post Helpful? 1
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 765
  • View blog
  • Posts: 2,227
  • Joined: 20-March 10

Re: Creating a class with dynamically allocated integers

Posted 15 January 2013 - 05:47 AM

View PostSkydiver, on 15 January 2013 - 12:11 PM, said:

As far as I know the Standard Template Libraries are not required to be thread safe. So using the vector within the class won't guarantee thread safety.


Ahh really,

I didn't know that I thought all the STL containers were thread safe.
I thought it was only in certain race conditions that they were considered to be not thread safe
but for normal activity they were thread safe.

would doing it like this be thread safe.

#include <vector>
#include <iostream>
#include <Windows.h>
using namespace std;


	class  dynamAlloc
{
vector<int> p;
CRITICAL_SECTION section;

public:
dynamAlloc();
~dynamAlloc();
void Set(int a)
{
	p.push_back(a);
}
int Get(int B)/>
{
return p[b];
}

};

int main()
{
	
	
	dynamAlloc var;
	for (int i = 0; i<20; i++)
	var.Set(i*2);
	
	int answer = var.Get(5);
	cout << answer;
	cin.get();
	return 0;
}

dynamAlloc::dynamAlloc()
{
	
	InitializeCriticalSection(&section);
	EnterCriticalSection(&section);
}

dynamAlloc::~dynamAlloc()
{
   LeaveCriticalSection(&section);
}


Was This Post Helpful? 1
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,742
  • Joined: 05-May 12

Re: Creating a class with dynamically allocated integers

Posted 15 January 2013 - 06:33 AM

Yes, that would be thread safe, but not thread friendly since you are holding on to the critical section for the entire life of the dynamAlloc instance. To make it better would be to enter and leave the critical section within the methods that touch the STL container. Get() will enter and leave. Set() will enter and leave.
Was This Post Helpful? 1
  • +
  • -

#11 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 765
  • View blog
  • Posts: 2,227
  • Joined: 20-March 10

Re: Creating a class with dynamically allocated integers

Posted 15 January 2013 - 06:49 AM

View PostSkydiver, on 15 January 2013 - 01:33 PM, said:

Yes, that would be thread safe, but not thread friendly since you are holding on to the critical section for the entire life of the dynamAlloc instance. To make it better would be to enter and leave the critical section within the methods that touch the STL container. Get() will enter and leave. Set() will enter and leave.


Aah thanks for the clarification,

I really will need to get round to reading Jeffrey Richters Book,

You really are a Gold Mine of information.

Thanks again.

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,742
  • Joined: 05-May 12

Re: Creating a class with dynamically allocated integers

Posted 15 January 2013 - 07:01 AM

On Windows, an InterlockedCompareAndExchange() would let you be thread safe without needing a critical section.
Was This Post Helpful? 1
  • +
  • -

#13 Agustinbmed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-January 13

Re: Creating a class with dynamically allocated integers

Posted 15 January 2013 - 07:34 AM

View Postsnoopy11, on 15 January 2013 - 06:49 AM, said:

View PostSkydiver, on 15 January 2013 - 01:33 PM, said:

Yes, that would be thread safe, but not thread friendly since you are holding on to the critical section for the entire life of the dynamAlloc instance. To make it better would be to enter and leave the critical section within the methods that touch the STL container. Get() will enter and leave. Set() will enter and leave.


Aah thanks for the clarification,

I really will need to get round to reading Jeffrey Richters Book,

You really are a Gold Mine of information.

Thanks again.

Snoopy.


You guys have been really helpful! Thanks also for mentioning a book name, I will see if I can get that reading too!

Appreciate it a lot!,

Agustin
Was This Post Helpful? 0
  • +
  • -

#14 Agustinbmed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-January 13

Re: Creating a class with dynamically allocated integers

Posted 15 January 2013 - 09:51 AM

View Postsnoopy11, on 15 January 2013 - 05:47 AM, said:

View PostSkydiver, on 15 January 2013 - 12:11 PM, said:

As far as I know the Standard Template Libraries are not required to be thread safe. So using the vector within the class won't guarantee thread safety.


Ahh really,

I didn't know that I thought all the STL containers were thread safe.
I thought it was only in certain race conditions that they were considered to be not thread safe
but for normal activity they were thread safe.

would doing it like this be thread safe.


dynamAlloc::dynamAlloc()
{
	
	InitializeCriticalSection(&section);
	EnterCriticalSection(&section);
}

dynamAlloc::~dynamAlloc()
{
   LeaveCriticalSection(&section);
}



Hello there again,

Reading from the link that you gave me (Snoopy), I arrived at a part where it specifies that you do have to delete your instance of critical section so my question is:

Would you put the delete at the:

dynamAlloc::~dynamAlloc()
{
   DeleteCriticalSection(&section);
}



Instead of the original (LeaveCriticalSection(&section);)?

Cheers,
Was This Post Helpful? 0
  • +
  • -

#15 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 765
  • View blog
  • Posts: 2,227
  • Joined: 20-March 10

Re: Creating a class with dynamically allocated integers

Posted 15 January 2013 - 10:19 AM

Yes,

That would seem the logical place for that :)

Snoopy
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1