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

I have some questions about multi threading .

I ve been given this question in a test and i failed to answer it because i wasnt that adept at multi threading . Can any of u guys explain it to me. THe question is below.
Expand|Select|Wrap|Line Numbers
  1. class Buffer {
  2.    static int dataBuffer[100];
  3.    static int head = 0;
  4.    static int tail = 0;
  5.  
  6.    public:
  7.    static void Produce(int data) {
  8.       dataBuffer[head++] = data;
  9.       if (head == 100) head = 0;
  10.    }
  11.    static bool CanConsume() {
  12.       if (head != tail) return true;
  13.       return false;
  14.    }
  15.    static int Consume() {
  16.       int pos = tail++;
  17.       if (tail == 100) tail = 0;
  18.       return dataBuffer[pos];
  19.    }
  20. }
  21. void Producer() {
  22.    while(1) {
  23.       … some stuff …
  24.       Buffer.Produce(x);
  25.       … some stuff …
  26.    }
  27. }
  28. void Consumer() {
  29.    while(1) {
  30.       … some stuff …
  31.       if(Buffer.CanConsume() ) {
  32.          int x = Buffer.Consume();
  33.          … do something with x …
  34.       }
  35.       … some stuff …
  36.    }
  37. }
  38.  
a. If we have a multithreaded system with one thread executing ‘Producer’ and one thread executing ‘Consumer’ is there anything wrong with the code above? Give details.




b. If we have two threads running ‘Producer’ and one thread running ‘Consumer’ is there anything wrong with the code above? Give details.




c. If we have one thread Running ‘Producer’ and two threads running ‘Consumer’ is there anything wrong with the code above? Give details.
Aug 26 '08 #1
6 1894
sicarie
4,677 Expert Mod 4TB
So, due to this site's policy on homework questions, let's start with your answers.

What do you think the answers to these are?
Aug 26 '08 #2
So, due to this site's policy on homework questions, let's start with your answers.

What do you think the answers to these are?
I think if we just use a single thread then te producer and consumer will get into a deadlock .

But if we use two threads of producer and one thread of consumer it will be fine.

Am i right ??
Aug 26 '08 #3
SpecialKay
109 100+
I as well dont know the answer, but i understand threading some what. If i had to guess. i think that you are right, single threaded the code would block in the endless loop. if both function were running at the same time, i think it would work alright. As for having 2 threads running consumer and likewise for producer, i could only guess it would work, but you might get some wierd values.

interesting tho, i look forward to the answer.
Aug 26 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
Any code using static variables cannot be multi-threaded. Those variables will be visible to every thread and their values cannot be relied upon unless thay are protected by a critical section, mutex, or some such.

I don't see any such protections in the code.

If you intend those static variables to be unique per thread, then don't make thenm static. Instead, have the thread create them on the heap and use the pointers in the thread code. Then just delete the pointers before you exit the thread.
Aug 26 '08 #5
JosAH
11,448 Expert 8TB
That buffer is the shared resource so it should be protected by a mutex (a binary
semaphore). A producer should wait as long as the buffer is full, a consumer should
wait as long as the buffer is empty. When any of them passes the mentioned
wait-loop, they should notify all the other waiting threads so they are able to do
whatever they want to do. It is multi-threading 101.

kind regards,

Jos
Aug 27 '08 #6
SpecialKay
109 100+
no where in the code is there a signal to the other thread. So that, plus the Static decliarations are the problems?
Aug 27 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
1
by: Carlos Kirkconnell | last post by:
I'm programming a multi - threaded application using C#. I have two questions regarding to the use of threads 1- I have a Hashtable that will have multiple writters and multiple readers. I used...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
0
by: Stuart Norris | last post by:
Dear Group, I am attempting to write a "splash" and "status" Form using a second thread. I wish to use this Form to display status information to the user when I do CPU intensive work in my...
0
by: Thomas B. Johansen | last post by:
Dear group i im trying make a small apps there is testing the ip numbers and domains name found in a mail header on one or more RBL servers and when doing this it is slow to wait for one ip...
13
by: notregister | last post by:
my program have as many as 8 threads running at a same time. can different threads open a same file at the same time? let say thread1 open a file abc.txt at the same time with thread 2, both doing...
8
by: bob | last post by:
I am unsure how to approach a C# windows App. Essentially I want to do this. I want to write an app that reads from database_A performs some calculations updates the pretty dials on the screen...
0
ammoos
by: ammoos | last post by:
hi friends pls help me.. i got an assignment which i feel very difficult to me.. i dont have more knowledge about multi-threading in .net... the assignment details is below.... pls help me... i...
5
by: George Maicovschi | last post by:
As multi-threading is not built in PHP I've been using a hack letting the Apache server handle the multi-threading issues, but I'm really curious of other approaches to this issue. If anyone has...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: 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...

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.