473,397 Members | 1,974 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,397 software developers and data experts.

mutex example

can any one give a simple example for implementing mutex.
I have 3 threads (taking different input files )enter into the same
function at different times...

The 3 threads need to exit/leave the function till all the input files
reached till end which are taken as input to the threds.
thanks to all

Jul 22 '08 #1
2 4577
fr*********@googlemail.com wrote:
can any one give a simple example for implementing mutex.
I have 3 threads (taking different input files )enter into the same
function at different times...

The 3 threads need to exit/leave the function till all the input files
reached till end which are taken as input to the threds.
At this point "mutex" or any other synchronisation object are platform-
specific. Consider asking your question in the newsgroup for your platform.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 22 '08 #2
"fr*********@googlemail.com" <fr*********@googlemail.comwrites:
can any one give a simple example for implementing mutex.
I have 3 threads (taking different input files )enter into the same
function at different times...

The 3 threads need to exit/leave the function till all the input files
reached till end which are taken as input to the threds.

So you want the three threads in the same function at the same time.
So you want absolutely no mutual exclusion, but exactly the opposite,
you want mutual INCLUSION!

You should try to express what you want more clearly. For example,
you could start with something more grammatically correct. I have
really a hard time parsing this sentence:

"The 3 threads need to exit/leave the function till all the input
files reached till end which are taken as input to the threds."
Let's assume what you want is:
thread 1
|
|
v
----- enter function
|
|
v
(go on reading file 1)
| thread 2
| |
| |
| v
| ----- enter function
| |
| |
| v
| (go on reading file 2)
| |
| |
| |
| | thread 3
| | |
| | |
| | v
| | ----- enter function
| | |
| | |
| | v
| | (go on reading file 3)
v | |
----- reach eof v |
| ----- reach eof |
| | v
| | ----- reach eof
| | |
v v v
---------------------------------------------------- exit function
| | |
| | |
v v v
The synchronization at the exit function could be implemented with a
condition.
initialize:
- a counter = 0
- a mutex m
- a condition c

synchronized_exit(n)
mutext_lock(m)
increment counter
if (counter >= n) then
condition_broadcast(c)
counter=0
else
condition_wait(c,m)
endif
mutext_unlock(m)
All three threads shall call synchronized_exit(3) after reaching their EOF.
man pthread_cond_init pthread_cond_wait pthread_cond_broadcast
man pthread_mutex_init pthread_mutex_lock pthread_mutex_unlock
--
__Pascal Bourguignon__
Jul 22 '08 #3

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

Similar topics

0
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
4
by: TGOS | last post by:
I was thinking about it for a while, a mutex written in C and without disabling any interrupts. Is it possible? typdef struct mutex { unsigned int owner1; unsigend int owner2; } *mutex; ...
3
by: smith4894 | last post by:
Hello, I have an application that essentially consists of two threads doing their things. One thread is a producer, and pushes bytes (of a struct) into a pipe, and another is a consumer that...
16
by: Ed Sutton | last post by:
I use a mutex to disallow starting a second application instance. This did not work in a release build until I made it static member of my MainForm class. In a debug build, first instance got...
2
by: Alex | last post by:
Dear colleagues I am programming an application which starts a lot of threads for some calculations. The problem that I have is that when I start up to 100 threads then everything works fine....
8
by: Dan Pavel | last post by:
Hi, I did not used Mutex before and I need now. I have an application that control some workflows on different machines. I use SNMP for that. The problem is that the thread used for starting an...
11
by: Lamont Sanford | last post by:
Given an object of type Mutex, what method or property should be called to determine if it is currently owned? I could call WaitOne(0,false) -- and if the return value is false, I could deduce...
2
by: Jeroen | last post by:
Hi all, I've been trying to get my head around threading. Here's an example from the book I'm reading: /***************************/ Mutex m = null; const string name = "xyz"; try
45
by: Chris Forone | last post by:
hello group, is there a chance for other functions to get the lock if i have following loop: while (running) { Lock local(mutex); }
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.