The problem is i don't want to use local vars , is ther anyway do end the loop safely(removing all the vars used in function and etc) without using a local var ?
how do I stop and restart a Thread in C# once it reaches a certain location on a form then restart the thread once it goes outside of the location boundries?
I am trying to stop an animation thread (a bullet) once it reaches a block on a panel.
I wanted to input some numbers with scanf function, i can enter some numbers and if I input -1 to the scanf, the input must end. And the scanf function has limited input, the max that I can input is 40 numbers.example if enter 1 2 4 6 5 4 -1 the scanf function will ended and the result will be appear.I wanted to know how the scanf function is like that would be best for this problem, Code: scanf("%d", &n); the result if I input those number will be like
I have a program that I made, which does a list of stuff, then checks if a key is pressed, and if it's pressed, stop the looping.
The problem is, it checks if the key is pressed once a loop, which means that if the list has delays, you have to hold the button down for a while (depending on the list).
Here's an example: Move mouse; Pause program for 2 seconds; Move mouse; Pause program for 2 seconds; Check if key is pressed;
The code below gives me the maximum odd number and the minimum even number of a list of numbers that the user type. However, for the loop to stop the user needs to type 1000. Is there a better way (without having to have a "stopping" number, just in case I need this number to be somewhere on the list) to get the same results?
Code: #include <iostream> #include <iomanip> #include <cmath> #include <string> using namespace std; int main() { int x, maxi, mini;
Write a program that reads numbers from cin and then sums them, stopping when 0 has been entered. Add the following to your program:
-A counter within the while loop that keeps track of the number of inputs entered. -A comment that determines if the total value is less than 100 or greater than 100, as in the sample output -Switch statements to determine the total number of inputs
Sample output:
Enter numbers, one per line. Enter 0 to end: 7 8 6 5 5 9 8 0
The total was 48. The total number of inputs read is 8. The total is less than 100.
The code that I've got so far lets me keep track of the inputs entered, and displays the total of the values, but I'm not sure how to do the part where it determines if the value is greater or less than 100.
#include <iostream> using namespace std; int main() { int val, total=0; int i=0; cout << "Enter numbers, one per line. Enter 0 to end: ";
So, I'm going to write a recursive function for a asterisk pattern but I'm stuck figuring out how to create a stopping case for it, better yet, I'm unable to describe the pattern completely.
* Every odd row has 1 * with 1 incremented white space
* Every "pair" of asterisks equals 8 total (EX. 8 one pair *'s, 4 two pair *'s, 2 four pair *'s)
Unfortunately, that's all I got. how I can represent this as I function. Once I figure out what my stopping case should be, I think I can do the coding on my own.
I run the program with gdb , i searched but find nothing about how i could run gdb , that shows what line of code is running constantly (i rather it also shows value of the variable on each line if it's possible ) without stopping (i mean i don't want to enter "step" every time , i just need to run the program with debugger and shows line of the code is running (without need to enter step each time by myself)is it possible? if yes , what command is needed to start gdb for this purpose?
P.S: for this purpose if i have to set breakpint i will. but even i rather not set breakpoint , i rather gdb while running the program shows what line is now executing (rather with the value of variabels).
I've been working on this project which inserts data for different types of books lately, and I'm trying to utilize inheritance and polymorphism. The issue I've been having is after I create an object with my MediaFactory, I go to insert data into that type of object, but it won't reach the correct child class. The parent class in this case is MediaData. The class I'm trying to reach is Childrens, and the class that falls in between is called Book. The function I'm calling to insert the information is setData(ifstream&). This function simply places the information from a txt file into an object with the insertion operator.
Right now the program runs into the setData function of Book instead of Childrens. I need Childrens so I can enter all the required attributes (title,author,year). The call to this function is in MediaManager through the function called buildMedia, and my test is running into the case if (type == 'Y'). From there a pointer of type MediaData is created and pointed to a new object returned of type Childrens. I'm using my debugger in Xcode which does show that the correct object type (Childrens) was created.
I've tried a couple things so far. First I created another function called getData. From there I passed in *media and infile (txt input file) into that function, which then passed it right into setData with a pointer to the object and infile in the parameter. That didn't work, so I also tried going back into Childrens and removing MediaData from all my virtual functions and replacing it with Book. I got the same result when that happened; It morphed into Book class instead.
I have a portion of my UML as a reference. Childrens is a Book; Book is a MediaData. I also included all code for MediaManager, MediaHash, MediaFactory, MediaData, Book, and Childrens.
I did not include the operator overloads in the .cpp files to eliminate some redundancy.
//----------------------------------------------------------------------------- // MediaManager.h // Manager class for MediaData type objects //-----------------------------------------------------------------------------
#ifndef MediaManager_H #define MediaManager_H #include "MediaHash.h" #include "MediaFactory.h" #include "MediaData.h" using namespace std; class MediaManager {
I'm working on coding a game of pong that allows players to play until one reaches a score of 10. Once the winning score is reached I want to display a "Game Over" message stating which player won. The game works perfectly except for stopping when the winning score is reached.
I found this program in a forum and it creates 1 to t threads with each thread pointing to the next and the last thread pointing to the first thread and it allows each thread to sequentially take a turn until all threads have taken n turns. That is when the program ends.
My doubt is if we see in void *tFunc(void *arg) function, first thread locks the mutex and then wait there forever till it's turn comes.Lets say if I spwan 1000 threads and they have take 10 turns each sequentially and what if a 550th thread locks the mutex as soon as the program starts and waiting for its turn which will never happen because still (turn=1) which means it's first thread's turn and first 549 threads have to complete their 1st turn before this 550 threda's turn comes.Doesnt this result in deadlock?
I had a requirement where i needed to create a thread and if the execution of thread is not completed in 5 minutes i needed to terminate its execution and continue with other part of the code.
I used the below code to create the thread
_beginthread(FuncnCall,0,NULL);
HANDLE hThread = GetCurrentThread();
Then after this code, I used the below code to check for 5 minutes
for (int i=1;i<=0;i++) { printf("Value of i=%d ",i); if(threadFinished) { break; } else { Sleep(1000); } }
After this if the value of "threadFinished" is false then i am terminating the thread like below
The Problem here is, after terminating the thread, the program abruptly closes by giving fatal error. Looks like memory leakage is happening after terminating the thread. Is it not the right way to safely exit the thread?
I am on a little project involving TCP socket-programming and multiple threads but need passing structures between two threads.
The first thread receives data with respective ID, temp, etc,.. Each ID is a list-item of a linked list and every ID has again a linked list of all the received data.
The second thread just have to access the list and perform some manipulations on it.
My solution: pass the head pointer of the linked list. Easy enough, right? Somehow, I am stuck receiving error message:" dereferencing pointer to incomplete type."
For the sake of ease and simplicity I just added a stripped down version of the project.
The magic (or not) happens in MainProcess.c: The Data thread should receive the pointer (a think) from the Connection thread.
I am doing something wrong since for me 4 threads perform 2 times slower then 1.I have 2 vectors with bunch of data to process, there is no concurrency (not moving elements and are independent of each other) so i just need to calculate some data from one and copy result in another.
I want to create two threads which will be calling even and odd functions where even function should print even number and odd function should print odd number.Can it be possible with condition variable? What is the code in both the cases i.e. two separate function and with condition variable.
void Arrival_MainThread::Body () { t1 = new boost::thread((Arrive_Fctor()), 2); t2 = new boost::thread((Arrive_Fctor()), 10000); t3 = new boost::thread((Arrive_Fctor()), 3000);
Code: class Arrive_Fctor { public: void operator()(int mean) { m_Arrivals = new Arrivals(mean); m_Arrivals->Activate(); #ifndef NO_RESOURCE
[code].....
The order of arrivals always appear as 3,2,1 it can't be 1,2,3 or 3,1,2 or something like that?
The body method is called when m_Arrival->Activate() is executed, and it is running as an independent thread.
Although MyThread1 and MyThread2 using critical section to protect the shared data m_Data, I will still suspend MyThread1 before accessing the shared data, to prevent any possible conflicts.
The problem is:
(1)When the first invoke of MyFun2, everything is OK, and the return value of MyFun2(that is nResult2) is 1 , which is expected.
(2)When the second, third and fourth invoke of MyFun2, the operations in MyFun2 are executed successfully, but the return value of MyFun2(that is nResult2) is a random value instead of the expected value 1. I try to using Debug to trace into MyFun2, and confirm that the last return statement is just return a value of 1, but the invoker will receive a random value instead of 1 when inspecting nResult2.
(3)After the fourth invoke of MyFun2 and return back to the next statement follow MyFun2, I will always get a "buffer overrun detected" error, whatever the next statement is.
I think this looks like a stack corruption, so try to make some tests:
1.I confirm the /GS (Stack security check) feature in the compiler is ON.
2.If MyFun2 is invoked after MyFun1 in MyThread1 is completed, then everything will be OK.
3.In debug mode, the codeline in MyFun2 that reads the shared data m_Data will not cause any errors or exceptions. Neither will the codeline in MyFun1 that writes the shared Data.
I have a vector that I would like to access and work with from multiple threads. I have created an example below to illustrate the functionality that I would like to accomplish.
The goals are to be (1) high speed, (2) thread safe, and (3) *if possible* continue to use vectors as my larger project uses vectors all over the place and as such I would like to maintain that.
However, if I need to switch from vectors to something else then I am open to that as well.
The following example below compiles but crashes rather quickly because it is not thread safe.
How I can fix the example below which I can then apply to my larger project?
I am trying to write a code that solves a system of linear equations such as A*B=C. My system has a dimension equal to 1600. The matrix A cab be separated into 4 sub matrices and each can be handled by a different thread. I tried to solve this using the following code:
int main() { int count = 0; //Inputing matrix A ifstream matrix; matrix.open("example.txt");
[Code] ....
Although the above code gives the correct answer, the time needs to find the solution is bigger than that needed without using threads.
I'm currently working on a server for handling clients in a 2d online game and I wrote some regular fstream file code for handling the file that stores their information and I was about to implement it into the server, then I realized there might be a problem having it open multiple times concurrently, so I googled it and came up with posts like
[URL]
I'm wondering if I can just treat it like everything else or will I have to do something specific for opening on multiple threads?
p.s. I did read those posts but I'm very new to multithreading
I have a school assignment to create a program that uses a producer thread to create a series of random numbers, and a consumer thread to read those numbers (via a shared bounded buffer), and record the min, max, average, and number of items. Now, the threads aren't synching well. Here's my code: