473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

using mutexes

Hi All,

Suppose that my program is such that
int counter;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
void *functionC()
{
pthread_mutex_lock( &mutex1 );
counter++;
printf("Counter value: %d\n",counter);
pthread_mutex_unlock( &mutex1 );
}

int main()
{
pthread_t thread_id;
pthread_create (&thread_id, NULL, &functionC, NULL);
//increment counter in main also

}
Will there be a problem when I do this?If the thread is schduled first
will the main() wait untill the thread unlocks?
If not,Is there anyway to lock the variable in the main program as
well as in the thread.

Cheers

Feb 12 '08 #1
4 1705
Will there be a problem when I do this?
No problems.

If the thread is schduled first
will the main() wait untill the thread unlocks?
No. To make the main thread wait you must make it try and take the
lock.

If not,Is there anyway to lock the variable in the main program as
well as in the thread.
Yes:
// Init counter.
int counter = 0;
// I hope this is some strange way of calling
// pthread_mutex_init()
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
void *functionC(void*)
{
inc_counter();
}

void inc_counter()
{
pthread_mutex_lock( &mutex1 );
counter++;
printf("Counter value: %d\n",counter);
pthread_mutex_unlock( &mutex1 );

}

int main()
{
pthread_t thread_id;
pthread_create (&thread_id, NULL, &functionC, NULL);

//increment counter in main also
inc_counter();
}
Feb 12 '08 #2
iceman wrote:
Hi All,

Suppose that my program is such that
int counter;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
May I suggest you ask on comp.programming.threads?

pthreads are topical there, but not here.

--
Ian Collins.
Feb 12 '08 #3
iceman wrote:
Will there be a problem when I do this?If the thread is schduled first
will the main() wait untill the thread unlocks?
The main thread will not wait but you can arrange that it does. If you
insert a pthread join operation, your main thread will wait until the
forked thread finishes.

int main()
{
pthread_t thread_id;
pthread_create (&thread_id, NULL, &functionC, NULL);
//increment counter in main also
pthread_join(thread_id, NULL);
}

Best Regards,
Szabolcs
Feb 12 '08 #4
On Feb 12, 6:07 am, iceman <jegan...@gmail.comwrote:
Suppose that my program is such that
int counter;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
void *functionC()
{
pthread_mutex_lock( &mutex1 );
counter++;
printf("Counter value: %d\n",counter);
pthread_mutex_unlock( &mutex1 );
}
int main()
{
pthread_t thread_id;
pthread_create (&thread_id, NULL, &functionC, NULL);
//increment counter in main also
}
That looks more like Posix C than anything C++. You really
should ask in a Posix (Unix) group, rather than here.
Will there be a problem when I do this?
Not if you synchronize the access to counter in main as well.
The Posix rule (which, I believe, will be the C++ rule when C++
adds threading in the next version) is simple: if any thread
modifies an object, and more than one thread accesses the
object, then all accesses must be externally synchronized.
If the thread is schduled first
will the main() wait untill the thread unlocks?
Why should it?
If not,Is there anyway to lock the variable in the main
program as well as in the thread.
The same way you locked it in the thread.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 12 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: rawCoder | last post by:
Hi All, I have a *.cer file, a public key of some one and I want to encrypt some thing using this public key. Can someone point me to a sample code for Encrypting some file using...
1
by: Mike | last post by:
When trying to compile (using Visual Web Developer 2005 Express Beta; frameworkv2.0.50215 ) the source code below I get errors (listed below due to the use of ICallBackEventHandler. Ultimately I...
7
by: Dhirendra Pal Singh | last post by:
Hi all, I have couple of questions, I hope I can find the answers here...(assuming the question are okay with this group) A) what is memory alignment? I have a rough idea but cant clearly...
5
by: Ken Varn | last post by:
If I have a value type such as int that I want to protect in a multi-threaded situation, Is it safe to use Monitor::Enter(__box(value))? I am thinking that a different object pointer is generated...
1
by: Graham | last post by:
Am I right to assume that you use Mutex(s) for protecting shared data within threads of an application; i.e. its not used to protect shared data between separate applications I have tried to use...
2
by: UJ | last post by:
Is there a way to get a list of all the mutexes that have already been defined? TIA - Jeff.
20
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
8
by: Ole Nielsby | last post by:
I want to create (with new) and delete a forward declared class. (I'll call them Zorgs here - the real-life Zorks are platform-dependent objects (mutexes, timestamps etc.) used by a...
3
by: pbd22 | last post by:
Hi. I have a C# program that fires an external VB6 program which writes to a file and terminates. It is ugly, but this is how I have to do it. I cannot change this part of the program. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.