C++ :: Create Two Threads Calling Even And Odd Functions

Mar 1, 2013

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.

View 6 Replies


ADVERTISEMENT

Visual C++ :: Can Two Functions In Same DLL Be Called By Two Threads?

Aug 27, 2014

I write a DLL MyDLL.dll with Visual C++ 2008, as follows:

(1)MFC static linked
(2)Using multi-thread runtime library.

In the DLL, this is a global data m_Data shared by two export functions, as follows:

ULONGLONG WINAPI MyFun1(LPVOID *lpCallbackFun1) {
...
Write m_Data(using Critical section to protect)

[Code]....

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.

View 4 Replies View Related

C++ :: Calling Template Functions

Feb 21, 2013

How do I call these functions from Mechanical.h???

Mechanical.h
#ifndef MECHANICAL_H_
#define MECHANICAL_H_
class statics { public:

template<class T> struct Inertia_types {
T Rec(T _x, T _y);
T Tri(T _x, T _y);

[Code] ...

I am trying to create templated functions which I can apply all data types to except for strings and other types such as wchar. Am I also writing these correctly? This is my first attempt ever doing this.

View 1 Replies View Related

C++ :: Calling Functions Of Class And Outside With Same Name?

Apr 6, 2013

Just a few moments ago i was just doing foolish things in c++ and discovered something new. Though some of you might have known this, for those who dont know, take a look at the follwing 2 small programs,

#include <iostream.h>
#include <conio.h>
void main();
void loop()

[Code]....

so here is my problem. i think u wud have figured out what m trying to do above. am actually calling the main() of the class and from there, i want to call the usual main... the problem is, during A.main()'s run, if i refer to main(); , that call represents itself, that it, it is like it calls itself. How on earth can i call the outside main?

View 7 Replies View Related

C++ :: Calling Functions From Templates?

Mar 14, 2013

Having issues calling my arguments from my templates, when i declare x and n in the main it comes up with errors

code below:

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <string.h>
using namespace std;
int intInput1, intInput2;

[code].....

View 4 Replies View Related

C :: Writing Factorials And Calling Other Functions To Do So

Jul 6, 2013

I am trying to get this statistical equation to work. m! / n! (m-n)!.

Code:
#include <stdio.h>
long factorial_M(long); // function prototype //
long factorial_N(long);
long total (long);
int main(int argc, const char * argv[]) {

[Code] .....

View 13 Replies View Related

C++ :: Calling Functions - Vector Of Pointers

Mar 27, 2013

I have a vector of pointers inside a seperate Exam class.

vector <Question* > question_list

The Question class is my base class in which I have derived sub classes for the different types of questions (MultipleChoice, LongAnswer, etc.). I am using my vector to hold the different types of questions.

in each of those classes I have virtual "write" functions in both the base and the derived classes, that write to a file differing for each type of question.

My problem now is calling the write function from a Exam function. I've tried several methods, such as:

for (size_t i = 0; i < question_list.size(); i++) {
question_list[i].write(testfile.c_str());
}

but it comes with two errors: "error C2228:left of '.write' must have class/struct/union" along with "IntelliSense: expression must have class type"

I have made a write function for the exam class as well but am not sure what it should include since the Exam class is not a derived class of the Question class.

View 15 Replies View Related

C++ :: Sinking Fund And Calling Functions

Nov 17, 2014

I have to do this assignment but really don't understand the calling and called functions yet. Rewrite your program for computing the value of a sinking fund so that there is a C++ function that is called to calculate the value, i.e., a function that returns the accumulated value based on the number of years with the annual interest rate compounded monthly and a fixed-size monthly deposit.

My program is:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
double R, r, t;

[code].....

View 1 Replies View Related

C++ :: Calling Functions From Text Files?

Jan 5, 2015

I'm working on a code that reads a text file and follows the instructions written in that file. An example of the text file:

*sum
11 4 61 2
1 2 0 14
17 99 1 1
*subtract
5 6 7
1 1 1
45 6 9

I want to read the text file line by line, each time I find an instruction (that is, the keywords *sum or *subtract) I want to make a summation (in the case of *sum) of all the numbers in each of the following lines, and repeat until the next instruction appear. So the text file above should generate:

78
17
118
-8
-1
30

That is:

11 + 4 + 61 + 2 = 78
1 + 2 + 0 + 14 = 17
17 + 99 + 1 + 1 = 118
5 - 6 - 7 = -8
1 - 1 - 1 = -1
45 - 6 - 9 = 30

how can I implement a dictionary(I'm guessing a dictionary is the best way to go here) so that when I read the string *sum i can call a function that does the summation of the following lines.

the size of the text file may be big (several mb) and the number of different instructions to search for may be in the magnitude of hundreds.

View 5 Replies View Related

C/C++ :: Calling Functions Inside A Class?

Nov 4, 2014

Im currently working on a class assignment and the pseudo code containing the instructions that I need to complete list:

//--------------------------------------------------------------------------------------------------------------
// CTOR: Point()
//
// DESCRIPTION
// Default constructor. Initializes the point to be at the origin (0, 0) and the color to black = (0, 0, 0).
//
// PSEUDOCODE
// Call Init() and pass 0, 0, 0, 0, and 0 as the parameters.
//--------------------------------------------------------------------------------------------------------------

My code for this is:

Point::Point(){
void Init(0, 0, 0, 0, 0);
}

The code I wrote for the function Init is here:

Point::Init(int mX, int mY, color mColor){
mX = 0;
mY = 0;
mColor = pInitColor;
}

My problem here is that whenever I try calling this function in the point class, I get an error next to void Init saying incomplete type is not allowed. Also visual studio is telling me that it expects a ')' after my first zero in that line.

View 1 Replies View Related

Visual C++ :: Mq4 Platform Calling Functions From DLL

Feb 10, 2013

I have Mq4 platform calling functions from DLL some how it doesnt make any differents when I make the call....

// DLL Code

#define WIN32_LEAN_AND_MEAN
#define MT4_EXPFUNC __declspec(dllexport)
using namespace std;
std::string My_String;
#define stringify(mymonths ) # mymonths

[Code] ....

I call this function

bool counts = StartRulls(bars);
Print("counts =",counts );
2013.02.09 23:03:242010.03.31 16:37 My_Mq4.mq4 EURCAD,M5: counts = 1

My result always stays 1

View 4 Replies View Related

C :: Calling Functions From Parallel (MPI) Fortran Program

Apr 24, 2013

I'm working on a parallel Fortran program by using MPI, which calls a very good random number generator function from C (drand48 and srand48 for seed).

My Fortran program file "Par_PICFort_4.f95" is smt. like:

Code: PROGRAM main
IMPLICIT NONE
Include 'mpif.h'

[Code]....

I compile the code as: Code: mpif77 -o PIC Par_PICFort_4.f95 drand48.c Program seem to be running, but after checking the simulation results, I see that drand48 is not working in a parallel way. Creating independent random numbers which different seeds is very important for me.

Question: Is it possible to run the drand48.c file as parallel?

View 2 Replies View Related

C :: Calling Functions With Arguments Using Pointer Variables As Operators

Feb 2, 2013

There are, or course, better ways to do this, but I need to stick to some rules:

(1) Use only pointer variables and not arrays or structs.
(2) Use the three functions shown--regardless of easier methods.

The program should ask for some input, operate on those numbers, and then display the results. I know I am confused over these things:

(1) All that syntax using '*' and '&' or neither.
(2) How to use the char type correctly.
(3) How to use a char type input as an operator (a + b).
(4) How to use the pointer of the operator variable (+,-,*,/) in an actual equation.

Code:
#include <stdio.h>
#include <stdlib.h>
// *** Prototype Functions ***
void Post_Results (float*);
void Calculate (float*, float*, char*, float*);
void Get_Numbers (float*, char*, float*);

[Code]......

View 5 Replies View Related

C++ :: Program To Calculate Company Weekly Payroll - Calling Functions?

Jun 9, 2014

This program in not completed. I am creating a large program in order to calculate a company's weekly payroll. For now I am filling in the separate functions piece by piece before the rest of the program is completed.

Right now I am trying to use separate functions to call other functions. I need to ask the user for the file name and then open the file. I am no longer getting any compiler errors however when I run the program all it displays is "Welcome."

So it's not actually calling the NameTheFile function and the OpenTheFile function. It just says "Welcome" and then closes down. Why is it doing this?

/*Program to determine company's weekly payroll*/

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void NametheFile() {

ifstream inputFile;

[Code] ....

View 1 Replies View Related

Visual C++ :: Window Created By Calling Create - DestroyWindow Function?

Jan 17, 2013

If a window is created by calling Create, the function DestroyWindow must be called to destroyed it at the end?

View 4 Replies View Related

C++ :: Calling Derived Class Functions In A Function With Parameter Of Base Class

Mar 30, 2013

Say I have 3 classes:

class Player {
public:
virtual func1();

[code]....

Say in my main class, I have a function fight(Player p1, Player p2) and I would like to do something like this in the fight function, given that p1 is the human and p2 is the computer:

//function fight()
fight(Player p1, Player p2) {
p1.func2();
}
//using function fight()
fight(human, computer);

When I compile the program, I got this: error: ‘class Player’ has no member named 'func2()' What can I do to allow p1 to call func2 inside fight()? I'm not allowed to use pointers as the parameter for fight() and have to use the signature fight(Player p1, Player p2).

View 6 Replies View Related

C :: How To Create Functions Such As Floats

Sep 16, 2014

I am new with C programming and trying to learn how to create functions such as floats. But for some reason when I try to compile this program the compiler will tell me Weight() is not a function.

Code:
#include <stdio.h>
float Weight(float Mass, float g)
{
float Weight = 0;
g = 9.81;

[Code] ....

View 3 Replies View Related

C :: How To Create Functions With Arrays

Jun 12, 2013

how to create functions with arrays :/

You have to write a single C program that creates a one dimensional array of size MAX(to be declared as a declarative constant using directive #define MAX 10), initializes its elements with a given value using the function InitArray, then populate the array with some random numbers between 1 and MAX inclusive, and finally search the array for certain keys.

The structure for this C program is as follows:

int main() {
//array A to be locally declared here
int
size = MAX;
}

[code]....

View 3 Replies View Related

C++ :: List Of Functions That Can Create A Unique ID

Nov 4, 2014

I'm working on a list of functions that can create a unique ID for a computer. Here's what i have so far:

GetAdapterInfo();
RegOpenKeyEx();
GetVolumeInformation():
CoCreateGUID();

Are there any other functions that do this?

View 1 Replies View Related

C :: Use SIGNALS To Control Threads

Nov 11, 2013

I've to use SIGNALS to control threads. Here's my code without the use of signals as I'm no good at that

Code:

#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
}

[code]....

View 4 Replies View Related

C++ ::  threads To Take Sequential Turns

Jun 30, 2014

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int turn = 1;
int counter = 0;
int t, n;

[Code] ....

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?

View 2 Replies View Related

C++ :: Encapsulate Two Threads In A Class?

Apr 29, 2014

how to encapsulate two threads in a class?

View 1 Replies View Related

C++ :: Stopping Threads Without Using Local Var?

Aug 31, 2014

Here's my code,

#include <iostream>
#include <unistd.h>
#include <thread>
using namespace std;
void Blink();

[Code] .....

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 ?

View 3 Replies View Related

C/C++ :: Suspending And Resuming Threads

Apr 18, 2014

#include <stdio.h>
#include <pthread.h>
int i; // loop control variable
int *id; // array used to pass parameter of id
int nThreads; // #threads

[Code]....

I want to know where I am going wrong. I am strong in java but not in c so that may be my problem.

View 9 Replies View Related

C++ :: Creating And Terminating Threads?

Aug 31, 2014

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

if(threadFinished == false) {
TerminateThread(hThread,0);
CloseHandle(hThread);
}

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?

View 2 Replies View Related

C++ :: OpenMP - Intervals Assigned To Threads?

Feb 6, 2014

Is there a way of knowing which indices a thread is assigned in a parallel openMP scope?

View 6 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved