Click here to Skip to main content
15,885,214 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello,

See the following code:

I am having some library file like libexample.so
And in my code i am using some function X(inputbuffer , outputbuffer)present in library


/*
Assume for each thread there are corresponding input buffers and output buffers
example if there are 50 threads 50 input buffers and out buffers are available
i.e input buffers are unique to each other so as output buffers....
*/
void* threadFunX(void *num)
{
Mutex lock();
int ret = X(inputBuffer,outputbuffer,length);
if(ret)
{
//store the output buffer in afile
}
else
error occured.....
Mutex unlock();
pthread_exit(NULL);
}

int main()
{
pthread_t thread1[100];

for(i=0;i<100;i++){
p1 = pthread_create(&thread1[i],NULL,threadFunX,(void *)i);
if(p1)
printf("Error occurred while creating thread.....\n");
//pthread_join( thread1[i], NULL);
}
for(i=0;i<100;i++)
pthread_join( thread1[i], NULL);

}


My question:
If we are not using Mutex we are getting output buffer corrupted.... for this reason we are using mutex....
Is there any alternative method other than mutex....
I want to use this library function efficiently.....
My main moto is I want to reduce running time taken by mutex method also...
if my code with mutex is running with 2 seconds..... I want better alternative method which takes lesser time than mutex with thread parallel programming...

Thanks in advance...
Posted
Comments
Sergey Alexandrovich Kryukov 22-Aug-14 1:22am    
Why, why alternative methods? Do you really understand the mutex functionality, as well as the thread?
First of all, you need to understand what to synchronize with what, what is your shared resource, if any.
Mutex cannot take any significant time, you 3 sec is absurd; this is not related to mutex performace. I have no idea what did you screw up. The protected fragment of code should be fast, otherwise it makes no sense.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900